<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/2/14
 * Time: 17:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Feedback extends Base
{
    /**
     * @return mixed|\think\response\Json
     * 首页展示
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $params = input('param.');
            $where = [];
            //状态筛选
            if(isset($params['feedback_status']) && $params['feedback_status'] != ''){
                $where['feedback_status'] = $params['feedback_status'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $lists = Db::name('feedback')
                ->join('up_u_user', 'feedback_user_id = user_id', 'left')
                ->field('feedback_id, feedback_content, feedback_dispose_content, feedback_time, 
                feedback_status, user_username, user_phone')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('feedback_time desc')
                ->select();
            $count = count($lists);//获取总条数
    
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * @return mixed|\think\response\Json
     * 回复处理意见
     */
    public function feedbackDispose(){
        if(Request::instance()->isPost()){
            $feedback_dispose_content = input('feedback_dispose_content');
            $feedback_id = input('feedback_id');
            $res = Db::table('up_feedback')
                ->where('feedback_id', $feedback_id)
                ->setField([
                    'feedback_dispose_content' => $feedback_dispose_content,
                    'feedback_status' => 2
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('id');
        $this->assign('feedback_id', $id);
        return $this->fetch('feedbackdispose');
    }
    
    /**
     * @return \think\response\Json
     * 删除意见反馈
     */
    public function feedbackDel(){
        $feedback_id = input('id');
        $res = Db::table('up_feedback')->delete($feedback_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}